<?php
/**
 * @version		1.0
 * @Project		Google Plus One Button
 * @author 		Sunento Wu, vivociti.com
 * @package		
 * @copyright	Copyright (C) 2011 vivociti.com. All rights reserved.
 * @license 	http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU/GPL version 2
*/

// Check to ensure this file is included in Joomla!
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );

class plgContentGoogle_Plus_One_Button extends JPlugin {

	function plgContentGoogle_Plus_One_Button( &$subject, $config )
	{
		parent::__construct( $subject, $config );
	}

	function onContentPrepare($context, &$article, &$params, $page=0)
	{	
		
		$document	= & JFactory::getDocument();
		$document = &JFactory::getDocument();
		$document->addScript( 'http://apis.google.com/js/plusone.js' );
		$view		= JRequest::getCmd('view');
		$position          = $this->params->get( 'position',  '' );
		
		if ( $view != 'article' ) { 
			return;	
		} else {
			$title= htmlentities( $article->title, ENT_QUOTES, "UTF-8");
			$url = $this->getPageUrl($article);
			
			if ($position == '1'){
				$article->text =  $this->getPlugInHTML($this->params, $article, $url, $title) . $article->text;  
			}
			if ($position == '2'){
				$article->text = $article->text . $this->getPlugInHTML($this->params, $article, $url, $title);
			}
			if ($position == '3'){
				$article->text =  $this->getPlugInHTML($this->params, $article, $url, $title) . $article->text . $this->getPlugInHTML($this->params, $article, $url, $title);
			}			
		}
			
	}
	
	private function  getPlugInHTML($params, $article, $url, $title) { 
		$category_tobe_excluded   = $this->params->get('category_tobe_excluded', '' );
		$content_tobe_excluded           = $this->params->get('content_tobe_excluded', '' );
		$excludedCatList = @explode ( ",", $category_tobe_excluded );	
		$excludedContentList 	   = @explode ( ",", $content_tobe_excluded );		
		$cssStyle   = $this->params->get('cssStyle', '' );
		
		$button_type = $this->params->get('button_type', 'tall');  
		$count = $this->params->get('count', 'true');  
		$supportedLang = $this->params->get('supportedLang', 'en-US');  
		
		
		$moduleBy = $this->params->get('moduleBy'); 

		if ( in_array ( $article->id, $excludedContentList ) || in_array ( $article->catid, $excludedCatList ) ) return;
		$img_live_dir = 'http://vivociti.com/images/plus2x2.gif';
		$htmlAuthor = "<a href=\"http://vivociti.com/component/option,com_remository/Itemid,40/func,select/id,9/\" title=\"Get Google +1 (Google Plus One) Recommend Button for Joomla\" target=\"_blank\"><img src=\"$img_live_dir\" border=\"0\"/></a>"; 
		$htmlCode ="<!-- Place this tag in your head or just before your close body tag -->\n";
		$htmlCode ="<script>\n";
		$htmlCode ="{lang: '$supportedLang'}\n";
		$htmlCode ="</script>\n";
		$htmlCode .="<span style='$cssStyle'>";
		$htmlCode .= "<g:plusone count=\"$count\" size=\"$button_type\"></g:plusone>";
		if ($moduleBy == "no") {  
			$htmlCode .= $htmlAuthor;
		}
		$htmlCode .= "</span>";
		
     return $htmlCode; 
	}
	
	private function getPageUrl($obj)
	{
		if (!is_null($obj)) 
		{
			$url = JRoute::_(ContentHelperRoute::getArticleRoute($obj->slug, $obj->catslug, $obj->sectionid));
			$uri = JURI::getInstance();
      		$base  = $uri->toString( array('scheme', 'host', 'port'));
			$url = $base . $url;
			$url = JRoute::_($url, true, 0);
			return $url;
		}
	}
}
?>